/*
 * 座位管理，可能会涉及到房间管理，所以统称空间管理
 */
(function() {
	function can_manage_space(argument) {
		return this.getPerm().can_manage_space;
	};
	var skill = {
		canSpace: can_manage_space,
		_getSpace: function(space_id) {
			var space = BusSpaceCon.get(space_id);
			if (!space) {
				throwCusError("找不到指定空间")
			}
			if (space.bus_id !== this._id) {
				throwCusError("指定空间不属于商家")
			}
			return space;
		},
		//新增划分空间
		addSpace: function(space_info) {
			space_info.bus_id = this._id;
			var space = new BusSpaceCon(space_info);
			return space;
		},
		//修改空间信息
		updateSpace: function(space_id, space_info) {
			var space = this._getSpace(space_id);
			space.setInfo(space_info);
			midEvent.emit("bus.space.use_state.changed", this._id, space_id);
			return space;
		},
		removeSpace: function(space_id) {
			var space = this._getSpace(space_id);
			space.destroy();
		},
		getSpaceList: function() {
			return BusSpaceCon.findByBusId(this._id);
		},
		getSpaceListByType: function(type) {
			return BusSpaceCon.findList({
				bus_id: this._id,
				type: ~~type
			});
		},
		getSpaceListByUseState: function(use_state) {
			return BusSpaceCon.findList({
				bus_id: this._id,
				use_state: ~~use_state
			});
		}
	};
	module.exports = skill;
	var db = require("../db/index");
	var BinaCon = require("../bina/_con");
	var DomainCon = require("../domain/_con");
	var GoodsCon = require("../goods/_con");
	var UserCon = require("../user/_con");
	var BusCon = require("../bus/_con");
	var AchCon = require("../ach/_con");
	var ClassifyCon = require("../bus.classify/_con");
	var TagCon = require("../bus.tag/_con");
	var LeagueCon = require("../league/_con");
	var BuffCon = require("../buff/_con");
	var BusUserBindCon = require("../bus_user_bind/_con");
	var BusGuideCon = require("../bus.guide/_con");
	var BusSpaceCon = require("../bus.space/_con");
	var CommentCon = require("../comment/_con");
	var config = require("../_include/config");
	var data_cut = require("../_include/data_cut");
	var helper = require("../_include/helper");
	var md5 = require("../_include/md5");
	var $Object = require("../_include/$.Object");
	var $Array = require("../_include/$.Array");
	var tld = require('tldjs');
	var fs = require("fs");
	var Fiber = require('fibers');
	var midEvent = require("../../middleware/index");
}());